//
//  KSTabBarController.m
//  BasketBallLeagues
//
//  Created by kenshin on 4/23/17.
//  Copyright © 2017 kenshin. All rights reserved.
//

#import "KSTabBarController.h"

#import "TKHomeViewController.h"
#import "TKIssueViewController.h"
#import "TKMyMainController.h"
//#import "BLMessageManager.h"
#import "UIView+WZLBadge.h"
#import "WZLBadgeImport.h"
#import <Photos/Photos.h>
#import "IJPhotosAuthorization.h"

@interface KSTabBarController () <UITabBarControllerDelegate>
@property (nonatomic, strong) UITabBarItem *lastItem;

@end

@implementation KSTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName: [BLColorConfig blackColor], NSFontAttributeName: [UIFont systemFontOfSize:12.f]} forState:UIControlStateNormal];
//    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName: [UIColor jc_colorWithHexString:@"#00a0e9"], NSFontAttributeName: [UIFont systemFontOfSize:12.f]} forState:UIControlStateSelected];
    [self configTabBar];
    self.delegate = self;
    UIImage *image = [UIImage jc_createImageWithColor:[BLColorConfig lineColor]];
    image = [image jc_scaleToSize:CGSizeMake([UIWindow ks_screenWidth], 1.f/[UIScreen mainScreen].scale)];
    [self.tabBar setShadowImage:image];
    image = [UIImage jc_createImageWithColor:[UIColor whiteColor]];
    image = [image jc_scaleToSize:CGSizeMake([UIWindow ks_screenWidth], 49)];
    [self.tabBar setBackgroundImage:image];
//    if ([[BLMessageManager sharedInstance] hasMessageNotRead]) {
//        [self setupTabbarBadge];
//    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setupTabbarBadge) name:kNotificationRefreshTabbarBadge object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearTabbarBadge) name:kNotificationclearTabbarBadge object:nil];
}

- (void)setupTabbarBadge
{
    UITabBarItem *lastItem = self.tabBar.items.lastObject;
    //it is necessary to adjust badge position
    self.lastItem = lastItem;
    lastItem.badgeCenterOffset = CGPointMake(0, 0);
    [lastItem showBadgeWithStyle:WBadgeStyleRedDot value:1 animationType:WBadgeAnimTypeNone];
}

- (void)clearTabbarBadge {
    [self.lastItem clearBadge];
}

- (void)configTabBar
{
    TKHomeViewController *homepageVC = [[TKHomeViewController alloc] init];
    homepageVC.title = @"首页";
    KSNavigationController *homepageNavi = [[KSNavigationController alloc] initWithRootViewController:homepageVC];
    [self configTabbarItem:homepageVC.tabBarItem
                     image:[UIImage imageNamed:@"tab_home"]
             selectedImage:[UIImage imageNamed:@"tab_home_sel"]];
    
    TKIssueViewController *classifyVC = [[TKIssueViewController alloc] init];
    
    KSNavigationController *classifyNavi = [[KSNavigationController alloc] initWithRootViewController:classifyVC];
    [self configTabbarItem:classifyVC.tabBarItem
                     image:[UIImage imageNamed:@"tab_ issue"]
             selectedImage:nil];
    
    
    TKMyMainController *mineVC = [[TKMyMainController alloc] init];
    mineVC.title = @"我的";
    KSNavigationController *mineNavi = [[KSNavigationController alloc] initWithRootViewController:mineVC];
    [self configTabbarItem:mineVC.tabBarItem
                     image:[UIImage imageNamed:@"tab_center"]
             selectedImage:[UIImage imageNamed:@"tab_center_sel"]];
    
    self.viewControllers = @[homepageNavi, classifyNavi, mineNavi];
     
}

- (void)configTabbarItem:(UITabBarItem *)tabBarItem
                   image:(UIImage *)image
           selectedImage:(UIImage *)selectedImage
{
    tabBarItem.image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];;
    tabBarItem.selectedImage = [selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
}

#pragma mark - UITabBarControllerDelegate

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    if ([self.viewControllers indexOfObject:viewController] == 1) {
    
        PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
        if (status == PHAuthorizationStatusNotDetermined) {
            [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
                if (status == PHAuthorizationStatusAuthorized) {
                    dispatch_async(dispatch_get_main_queue(), ^{
                        TKIssueViewController *publishVC = [[TKIssueViewController alloc] init];
                        [UIViewController ks_pushViewController:publishVC animated:NO];
                    });
                    
                }
            }];
        } else if (status == PHAuthorizationStatusRestricted
                   || status == PHAuthorizationStatusDenied) {
            [IJPhotosAuthorization showSettingAlertWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
        } else {
            TKIssueViewController *publishVC = [[TKIssueViewController alloc] init];
            [UIViewController ks_pushViewController:publishVC animated:NO];
        }
        
        return NO;
    }
    if (self.shouldSelectTabIndex) {
        return self.shouldSelectTabIndex([self.viewControllers indexOfObject:viewController]);
    }
    return YES;
}


@end
